"""
Created on Nov, 2018

@author: hugo

"""
import gzip
import json
import numpy as np

def load_gzip_json(file):
    try:
        with gzip.open(file, 'r') as f:
            data = json.load(f)
    except Exception as e:
        raise e
    return data

# Print iterations progress
def printProgressBar(iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█'):
    """
    Call in a loop to create terminal progress bar
    @params:
        iteration   - Required  : current iteration (Int)
        total       - Required  : total iterations (Int)
        prefix      - Optional  : prefix string (Str)
        suffix      - Optional  : suffix string (Str)
        decimals    - Optional  : positive number of decimals in percent complete (Int)
        length      - Optional  : character length of bar (Int)
        fill        - Optional  : bar fill character (Str)
    """
    percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
    filledLength = int(length * iteration // total)
    bar = fill * filledLength + '-' * (length - filledLength)
    print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = '\r')

def dump_ndarray(data, path_to_file):
    try:
        with open(path_to_file, "wb") as f:
            np.save(f, data)
    except Exception as e:
        raise e


def load_ndarray(path_to_file):
    try:
        with open(path_to_file, "rb") as f:
            data = np.load(f)
    except Exception as e:
        raise e

    return data


def dump_ndjson(data, file):
    try:
        with open(file, "w") as f:
            for each in data:
                f.write(json.dumps(each) + "\n")
    except Exception as e:
        raise e


def load_ndjson(file, return_type="array"):
    if return_type == "array":
        return load_ndjson_to_array(file)
    elif return_type == "dict":
        return load_ndjson_to_dict(file)
    else:
        raise RuntimeError("Unknown return_type: %s" % return_type)


def load_ndjson_to_array(file):
    data = []
    try:
        with open(file, "r") as f:
            for line in f:
                data.append(json.loads(line.strip()))
    except Exception as e:
        raise e
    return data


def load_ndjson_to_dict(file):
    data = {}
    try:
        with open(file, "r") as f:
            for line in f:
                data.update(json.loads(line.strip()))
    except Exception as e:
        raise e
    return data


def dump_json(data, file, indent=None):
    try:
        with open(file, "w") as f:
            json.dump(data, f, indent=indent)
    except Exception as e:
        raise e


def load_json(file):
    try:
        with open(file, "r") as f:
            data = json.load(f)
    except Exception as e:
        raise e
    return data
